import ipaddress

def convert_subnet_to_hosts(subnet):
    network = ipaddress.ip_network(subnet)
    hosts = [str(ip) for ip in network.hosts()]
    return hosts

# 读取源文件
filename = "VLSM.txt"
output_filename = "HOST.txt"
all_hosts = set()  # 使用集合去除重复的IP地址

with open(filename, "r") as file:
    subnets = file.read().split(",")

# 转换为可用主机地址
for subnet in subnets:
    subnet = subnet.strip()
    hosts = convert_subnet_to_hosts(subnet)
    all_hosts.update(hosts)  # 使用update方法将新的IP地址添加到集合中

# 将主机地址排序
sorted_hosts = sorted(all_hosts, key=lambda x: ipaddress.ip_address(x))

# 写入目标文件
with open(output_filename, "w") as file:
    file.write("\n".join(sorted_hosts))

print("转换完成，结果已写入 HOST.txt 文件。")